package com.site.blog.my.core.service.impl;

import com.site.blog.my.core.dao.AdminUserMapper;
import com.site.blog.my.core.entity.AdminUser;
import com.site.blog.my.core.service.AdminUserService;
import com.site.blog.my.core.util.MD5Util;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class AdminUserServiceImpl implements AdminUserService {

    @Resource
    private AdminUserMapper adminUserMapper;

    @Override
    public AdminUser login(String userName, String password) {
        String passwordMd5 = MD5Util.MD5Encode(password, "UTF-8");
        return adminUserMapper.login(userName, passwordMd5);
    }

    @Override
    public AdminUser getUserDetailById(Integer loginUserId) {
        return adminUserMapper.selectByPrimaryKey(loginUserId);
    }

    @Override
    public Boolean updatePassword(Integer loginUserId, String originalPassword, String newPassword) {
        // 查出user
        AdminUser user = adminUserMapper.selectByPrimaryKey(loginUserId);
        if (user == null) return false;
        // 对比原密码是否一致
        originalPassword = MD5Util.MD5Encode(originalPassword,"UTF-8");
        if (originalPassword.equals(user.getLoginPassword())){
            user.setLoginPassword(MD5Util.MD5Encode(newPassword,"UTF-8"));
            // 一致的话就更新为新密码
            if (adminUserMapper.updateByPrimaryKeySelective(user) > 0) return true;
        }
        return false;
    }

    @Override
    public Boolean updateName(Integer loginUserId, String loginUserName, String nickName) {
        AdminUser user = adminUserMapper.selectByPrimaryKey(loginUserId);
        if (user != null){
            user.setLoginUserName(loginUserName);
            user.setNickName(nickName);
            if (adminUserMapper.updateByPrimaryKeySelective(user) > 0) return true;
        }
        return false;
    }
}
